// 测量文本宽高的函数
export const measureTextTarget = (text: string, fontSize: number = 20, fontFamily: string = 'PingFang SC'): { width: number; height: number } => {
    // 创建离屏 canvas
    const canvas = document.createElement('canvas');
    const ctx = canvas.getContext('2d');

    if (!ctx) {
        throw new Error('无法获取 canvas 上下文');
    }

    // 设置字体样式
    ctx.font = `${fontSize}px ${fontFamily}`;

    // 获取文本度量信息
    const metrics = ctx.measureText(text);

    // 计算文本尺寸
    const width = Math.ceil(metrics.width);
    const height = Math.ceil(
        metrics.actualBoundingBoxAscent +
        metrics.actualBoundingBoxDescent
    );

    // 清理 canvas
    canvas.remove();

    return { width, height };
}
